﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace Tmib_Video_Downloader_Sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

                string[] videoUrls;
        //List<string> downVideoUrls = new List<string>();
        List<YouTubeVideoQuality> downVideoUrls = new List<YouTubeVideoQuality>();


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                YouTubeVideoQuality tempItem = downVideoUrls[quality_ComboBox.SelectedIndex];

                saveFileDialog1.Filter = String.Format("{0} Files|*.{1}", tempItem.Extention.ToUpper(), tempItem.Extention.ToLower());
                //saveFileDialog1.FileName = String.Format("{0}.{1}", tempItem.VideoTitle, tempItem.Extention.ToLower());
                saveFileDialog1.FileName = FormatTitle(tempItem.VideoTitle);

                if (DialogResult.OK != saveFileDialog1.ShowDialog(this)) return;
                new frmFileDownloader(tempItem.DownloadUrl, saveFileDialog1.FileName).Show(this);
            }
            catch (Exception ex) { MessageBox.Show(this, ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = YouTubeDownloader.GetYouTubeVideoUrls(videoUrls);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                UseWaitCursor = false;
                if (e.Error != null)
                    throw e.Error;

                List<YouTubeVideoQuality> urls = e.Result as List<YouTubeVideoQuality>;

                TimeSpan videoLength = TimeSpan.FromSeconds(urls[0].Length);
                if (videoLength.Hours > 0)
                { drawVideoLenght(String.Format("{0}:{1}:{2}", videoLength.Hours, videoLength.Minutes, videoLength.Seconds)); }
                else
                { drawVideoLenght(String.Format("{0}:{1}", videoLength.Minutes, videoLength.Seconds)); };

                foreach (var item in urls)
                {
                    string videoExtention = item.Extention;
                    string videoDimension = formatSize(item.Dimension);
                    string videoSize = formatSizeBinary(item.VideoSize);
                    //string videoTitle = item.VideoTitle.Replace(@"\", "").Replace("&#39;", "'").Replace("&quot;", "'").Replace("&lt;", "(").Replace("&gt;", ")").Replace("+", " ");

                    quality_ComboBox.Items.Add(String.Format("{0} ({1}) - {2}", videoExtention.ToUpper(), videoDimension, videoSize));
                    quality_ComboBox.Text = quality_ComboBox.Items[0].ToString();
                    quality_ComboBox.Enabled = true;
                    downVideoUrls.Add(item);
                    
                    url_TextBox.Enabled = true;
                    timer1.Enabled = true;
                    progressBar1.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException.Message, ex.InnerException.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
         
                url_TextBox.Enabled = true;
                timer1.Enabled = true;
                progressBar1.Hide();
            }
        }
        }
    }
}
